package com.suliao.web.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.suliao.common.annotation.LoginPass;
import com.suliao.common.base.Result;
import com.suliao.common.base.UserContext;
import com.suliao.common.util.CryPtoUtil;
import com.suliao.common.util.OSSUtil;
import com.suliao.common.util.RedisUtil;

import javax.annotation.Resource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Null;

/**
 * @author ：M1nG
 * @date ：Created in 2020/11/6 10:46
 * @description：
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "/")
@LoginPass
public class LoginController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private OSSUtil ossUtil;

    @Autowired
    private CryPtoUtil cryPtoUtil;


    @RequestMapping("setMsg/{msg}")
    @ResponseBody
    public String getMsg(HttpSession session, @PathVariable("msg") String msg) {
        session.setAttribute("msg", msg);
        return "ok";
    }

    @RequestMapping("getMsg")
    public String getMsg(HttpSession session) {
        String msg = (String) session.getAttribute("msg");
        return msg;
    }

    @RequestMapping(value = {"/login/{back}", "/login"})
    public String login(@PathVariable(value = "back", required = false) String back, Model model) {
        model.addAttribute("backUrl", back);
        return "login";
    }

    @PostMapping("/user/login")
    @ResponseBody
    public Result userLogin(@RequestParam("loginName") String loginName, @RequestParam("password") String password, HttpSession session) {
        Result<Object> result = Result.ok();
        //service 查询user
        Object user = null;
        if (user == null) {
            result = Result.error("账户名不存在！");
        }
        // String encode = Base64.decodeStr(password);
        // String s = SecureUtil.md5(encode + user.getSalt());
        // if (!s.equals(user.getPassword())) {
        //    result = Result.error("账户名或密码错误！");
        // } else {
        //   UserContent userContent = userContentService.getUserContent(one.getUserId());
        UserContext userContext = new UserContext();
        //   session.setAttribute("user-info", cryPtoUtil.decrypt(JSONUtil.toJsonStr(userContext)));
        //   result.setData(userContent.getUserName());
        //  }
        return result;
    }


}
